package com.eightywork.life.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.eightywork.life.model.Space;
import com.eightywork.life.model.Xiaoqu;
import com.eightywork.life.util.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

public class DoService extends BaseService {
	static final String URL = DOMAIN + "dapi/do.php";

	/**
	 * 1. 20 获取小区物业分布信息（期、栋、层、房号）接口
	 * 
	 * @param xiaoquid
	 *            小区的id
	 * @return 期 qilist、栋 donglist、层 cenglist、房号 roomlist
	 * @throws MyHttpException
	 */
	public static Map<String, List<Map<String, String>>> xiaoquStru()
			throws MyHttpException {
		long start = System.currentTimeMillis();
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "xiaoqustru");
		map.put("xiaoquid", CacheService.getSpace().getXiaoquid());
		map.put("client", "android");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		Map<String, List<Map<String, String>>> dataMap = new HashMap<String, List<Map<String, String>>>();
		try {
			String json = HttpUtil.get(URL, map);
			List<Map<String, String>> list;
			Gson gson = new GsonBuilder().serializeNulls().create();
			JSONObject data = new JSONObject(json).getJSONObject("data");
			list = gson.fromJson(data.getString("qilist"),
					new TypeToken<List<Map<String, String>>>() {
					}.getType());
			dataMap.put("qilist", list);
			list = gson.fromJson(data.getString("donglist"),
					new TypeToken<List<Map<String, String>>>() {
					}.getType());
			dataMap.put("donglist", list);
			list = gson.fromJson(data.getString("cenglist"),
					new TypeToken<List<Map<String, String>>>() {
					}.getType());
			dataMap.put("cenglist", list);
			list = gson.fromJson(data.getString("roomlist"),
					new TypeToken<List<Map<String, String>>>() {
					}.getType());
			dataMap.put("roomlist", list);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		Log.d("xiaoquStru", System.currentTimeMillis() - start + "");
		return dataMap;
	}

	/**
	 *1.1.01  获得周边小区接口 keyword: 小区名称 longitude: 小区经度 latitude: 小区维度 span: 经纬度范围【默认0.01】
	 * 
	 * @param keyword
	 * @param longitude
	 * @param latitude
	 * @param span
	 * @return
	 * @throws MyHttpException
	 */
	public static List<Xiaoqu> xiaoquList(String keyword, String longitude,
			String latitude, String span) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "xiaoqu");
		map.put("op", "getlist");
		map.put("keyword", keyword);
		map.put("longitude", longitude);
		map.put("latitude", latitude);
		map.put("span", span);
		map.put("client", "android");
		if (CacheService.isLogined()) {
			map.put("m_auth", CacheService.getSpace().getmAuth());
		} else {
			map.put("m_auth", "");
		}
		List<Xiaoqu> list = null;
		try {
			String json = HttpUtil.get(URL, map);
			String data = new JSONObject(json).optString("data");
			Gson gson = new GsonBuilder().serializeNulls().create();
			list = gson.fromJson(data, new TypeToken<List<Xiaoqu>>() {
			}.getType());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 
	 * @param keyword
	 * @return
	 * @throws MyHttpException
	 */
	public static List<Xiaoqu> xiaoquSearch(String keyword) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "xiaoqu");
		map.put("op", "search");
		map.put("keyword", keyword);
		map.put("client", "android");
		if (CacheService.isLogined()) {
			map.put("m_auth", CacheService.getSpace().getmAuth());
		} else {
			map.put("m_auth", "");
		}
		List<Xiaoqu> list = null;
		try {
			String json = HttpUtil.get(URL, map);
			Log.d("json", json);
			String data = new JSONObject(json).optString("data");
			Gson gson = new GsonBuilder().serializeNulls().create();
			list = gson.fromJson(data, new TypeToken<List<Xiaoqu>>() {
			}.getType());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 赞列表接口 id: 被赞的id idtype: 被赞的idtype page: 当前页 perpage:
	 * 
	 * @param id
	 * @param idtype
	 * @param page
	 * @param perpage
	 * @return
	 * @throws MyHttpException
	 */
	public static List<Space> showgoodlist(String id, String idtype)
			throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "showgood");
		map.put("op", "toplist");
		map.put("id", id);
		map.put("idtype", idtype);
		map.put("m_auth", CacheService.getSpace().getmAuth());
		map.put("client", "android");
		List<Space> list = null;
		try {
			String json = HttpUtil.get(URL, map);
			String data = new JSONObject(json).optString("data");
			Gson gson = new GsonBuilder().serializeNulls().create();
			list = gson.fromJson(data, new TypeToken<List<Space>>() {
			}.getType());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 赞接口
	 * @param id
	 * @param idtype
	 * @return
	 * @throws MyHttpException
	 */
	public static String makeshowgood(String id, String idtype)
			throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "showgood");
		map.put("op", "make");
		map.put("id", id);
		map.put("idtype", idtype);
		map.put("m_auth", CacheService.getSpace().getmAuth());
		map.put("client", "android");
		try {
			String json = HttpUtil.get(URL, map);
			String data = new JSONObject(json).optString("msg");
			return data;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 注册获取验证码接口
	 * 
	 * @param username
	 *            用户名（手机号）
	 * @throws MyHttpException
	 */
	public static JSONObject getSeccode(String username) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", username);
		map.put("client", "android");	
		map.put("m_auth", "");
		try {
			String json = HttpUtil.post(URL+"?ac=register&op=getseccode", map);
			JSONObject jsonObject = new JSONObject(json);
			return jsonObject;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 2.2  注册接口
	 * 
	 * @param username
	 *            注册用户名(手机号)
	 * @param name
	 *            用户昵称
	 * @param password
	 *            密码
	 * @param seccode
	 *            手机验证码
	 * @param xiaoquid
	 *            小区
	 * @param sina_uid
	 *            【可选】
	 * @param sina_token
	 *            【可选】
	 * @param sina_expires_in
	 *            【可选】新浪微博token过期时间(格式：2013-05-12 22:59:59)
	 * @param qq_openid
	 *            【可选】
	 * @param qq_token
	 *            【可选】
	 * @return
	 * @throws MyHttpException
	 */
	public static Space register(String username, String name, String password,
			String seccode, String xiaoquid, String sina_uid,
			String sina_token, String sina_expires_in, String qq_openid,
			String qq_token) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "register");
		map.put("username", username);
		map.put("name", name);
		map.put("password", password);
		map.put("seccode", seccode);
		map.put("xiaoquid", xiaoquid);
		map.put("sina_uid", sina_uid);
		map.put("sina_token", sina_token);
		map.put("sina_expires_in", sina_expires_in);
		map.put("qq_openid", qq_openid);
		map.put("qq_token", qq_token);
		map.put("client", "android");	
		map.put("m_auth", "");
		Space object = null;
		try {
			String json = HttpUtil.post(URL, map);
//			System.out.println("map====>" + map.toString());
//			System.out.println("json====>" + json.toString());
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
//				System.out.println("=========================!!!!");
				String data = jsonObject.optString("data");
				Gson gson = new GsonBuilder().serializeNulls().create();
				object = gson.fromJson(data, Space.class);
			} else {
//				System.out.println("========msgkey=================!!!!" + jsonObject.optString("msgkey"));
//				System.out.println("========msg=================!!!!" + jsonObject.optString("msg"));
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return object;
	}

	/**
	 * 普通登录接口 username: 账号 password: 密码 iscookie: 是否保存密码，为1时保存
	 * 
	 * @param username
	 * @param password
	 * @param iscookie
	 * @return
	 * @throws MyHttpException
	 */
	public static Space login(String username, String password, String iscookie)
			throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "login");
		map.put("username", username);
		map.put("password", password);
		map.put("iscookie", iscookie);
		map.put("client", "android");	
		map.put("m_auth", "");
		Space object = null;
		try {
			String json = HttpUtil.post(URL, map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				String data = jsonObject.optString("data");
				Gson gson = new GsonBuilder().serializeNulls().create();
				object = gson.fromJson(data, Space.class);
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return object;
	}

	/**
	 * 新浪微博登录
	 * 
	 * @param name
	 *            新浪微博昵称
	 * @param sina_uid
	 * @param sina_token
	 * @param sina_expires_in
	 *            新浪微博token过期时间(格式：2013-05-12 22:59:59)
	 * @param avatar
	 *            新浪微博的头像(如果有传这个参数，会更新用户头像)
	 * @return
	 * @throws MyHttpException
	 */
	public static Space loginByWeibo(String name, String sina_uid,
			String sina_token, String sina_expires_in, String avatar)
			throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "login");
		map.put("logintype", "weibo");
		map.put("name", name);
		map.put("sina_uid", sina_uid);
		map.put("sina_token", sina_token);
		map.put("sina_expires_in", sina_expires_in);
		map.put("avatar", avatar);
		map.put("client", "android");	
		map.put("m_auth", "");
		Space object = null;
		try {
			String json = HttpUtil.post(URL, map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				String data = jsonObject.optString("data");
				Gson gson = new GsonBuilder().serializeNulls().create();
				object = gson.fromJson(data, Space.class);
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return object;
	}

	/**
	 * 
	 * @param name
	 *            腾讯微博的昵称
	 * @param qq_openid
	 * @param qq_token
	 * @param avatar
	 *            腾讯微博的头像(如果有传这个参数，会更新用户头像)
	 * @return
	 * @throws MyHttpException
	 */
	public static Space loginByQQ(String name, String qq_openid,
			String qq_token, String avatar) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "login");
		map.put("logintype", "qq");
		map.put("name", name);
		map.put("qq_openid", qq_openid);
		map.put("qq_token", qq_token);
		map.put("avatar", avatar);
		map.put("client", "android");	
		map.put("m_auth", "");
		
		Space object = null;
		try {
			String json = HttpUtil.post(URL, map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				String data = jsonObject.optString("data");
				Gson gson = new GsonBuilder().serializeNulls().create();
				object = gson.fromJson(data, Space.class);
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return object;
	}

	/**
	 * 找回密码接口(第一步)
	 * 
	 * @param username
	 *            用户名（手机号）
	 * @return
	 * @throws MyHttpException
	 */
	public static String lostpasswd1(String username) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "lostpasswd");
		map.put("username", username);
		map.put("lostpwsubmit", "1");
		map.put("client", "android");	
		map.put("m_auth", "");
		try {
			String json = HttpUtil.post(URL, map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				return jsonObject.optJSONObject("data").optString("uid");
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 找回密码接口(第二步)
	 * 
	 * @param uid
	 *            第一步返回的uid
	 * @param newpasswd1
	 *            新密码
	 * @param newpasswd2
	 *            确认新密码
	 * @param code
	 *            验证码
	 * @throws MyHttpException
	 */
	public static void lostpasswd2(String username, String uid,
			String newpasswd1, String newpasswd2, String code)
			throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "lostpasswd");
		map.put("uid", uid);
		map.put("username", username);
		map.put("newpasswd1", newpasswd1);
		map.put("newpasswd2", newpasswd2);
		map.put("code", code);
		map.put("resetsubmit", "1");
		map.put("client", "android");	
		map.put("m_auth", "");
		try {
			String json = HttpUtil.post(URL, map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") != 0) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 退出登录接口
	 * 
	 * @throws MyHttpException
	 */
	public static void loginout() throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "logout");
		map.put("m_auth", CacheService.getSpace().getmAuth());
		map.put("client", "android");	
		try {
			String json = HttpUtil.get(URL, map);
			JSONObject jsonObject = new JSONObject(json);
	//		System.out.println("jsonObject.optBoolean---------->" + jsonObject.optBoolean("error"));
			if (jsonObject.optBoolean("error")) {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			} 
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 赞接口
	 * 
	 * @param id
	 *            被赞的id
	 * @param idtype
	 *            被赞的idtype
	 * @throws MyHttpException
	 */
	public static int makeShowgood(String id, String idtype)
			throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		map.put("idtype", idtype);
		map.put("m_auth", CacheService.getSpace().getmAuth());
		map.put("client", "android");	
		try {
			String json = HttpUtil.post(URL+"?ac=showgood&op=make", map);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getInt("error") == 0) {
				return jsonObject.optJSONObject("data").optInt("result");
			} else {
				throw new MyHttpException(jsonObject.optString("msgkey"),
						jsonObject.optString("msg"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 赞列表接口
	 * @param id
	 * @param idtype
	 * @param page
	 * @param perpage
	 * @return
	 * @throws MyHttpException
	 */
	public static JsonObject showgoodToplist(String id,String idtype,String page,String perpage) throws MyHttpException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ac", "showgood");
		map.put("op", "toplist");
		map.put("id", id);
		map.put("idtype", idtype);
		map.put("page", page);
		map.put("perpage", perpage);
		map.put("m_auth", CacheService.getSpace().getmAuth());
		map.put("client", "android");	
		JsonObject object = null;
		try {
			String json = HttpUtil.get(URL, map);
			String data = new JSONObject(json).optString("data");
			Gson gson = new GsonBuilder().serializeNulls().create();
			return gson.fromJson(data, JsonObject.class);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return object;
	}
	
}
